package com.example.idachuappone.order.item;

import java.io.Serializable;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.example.idachuappone.BaseActivity;
import com.example.idachuappone.R;
import com.example.idachuappone.order.adapter.ImageGridAdapter;
import com.example.idachuappone.order.adapter.ImageGridAdapter.TextCallback;
import com.example.idachuappone.order.entity.ImageBucket;
import com.example.idachuappone.order.entity.ImageItem;
import com.example.idachuappone.order.imageutil.Bimp;
import com.example.idachuappone.utils.MainToast;

public class ImageGridActivity extends BaseActivity implements OnItemClickListener, OnClickListener {
	private ImageGridAdapter adapter;
	private int albumRequest;
	private Button btn_back;
	private Button btn_index;
	private GridView gridView;
	private int index;
	private List<ImageBucket> listBuckets;
	private List<ImageItem> listItems;
	private LinearLayout ll_ok;
	Handler mHandler;
	private TextView tv_num;

	public ImageGridActivity() {
		this.mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case BDLocation.TypeNone /* 0 */:
					MainToast.show(ImageGridActivity.this, "\u6700\u591a\u9009\u62e910\u5f20\u56fe\u7247", 0);
				default:
				}
			}
		};
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_image_grid);
		this.listBuckets = (List) getIntent().getSerializableExtra("listBuckets");
		this.index = getIntent().getIntExtra("index", 0);
		this.listItems = this.listBuckets.get(this.index).imageList;
		initView();
	}

	private void initView() {
		this.btn_back = (Button) findViewById(R.id.btn_back);
		this.btn_back.setOnClickListener(this);
		this.btn_index = (Button) findViewById(R.id.btn_index);
		this.btn_index.setOnClickListener(this);
		this.gridView = (GridView) findViewById(R.id.gridview);
		this.gridView.setOnItemClickListener(this);
		this.gridView.setSelector(new ColorDrawable(0));
		Collections.sort(this.listItems, new Comparator<ImageItem>() {
			Comparator com;

			{
				this.com = Collator.getInstance(Locale.CHINA);
			}

			@Override
			public int compare(ImageItem lhs, ImageItem rhs) {
				return this.com.compare(rhs.imageId, lhs.imageId);
			}
		});
		this.adapter = new ImageGridAdapter(this, this.listItems, this.mHandler);
		this.gridView.setAdapter(this.adapter);
		this.adapter.setTextCallback(new TextCallback() {
			@Override
			public void onListen(int count, List<ImageItem> list) {
				ImageGridActivity.this.tv_num.setText(new StringBuilder(String.valueOf(count)).toString());
			}
		});
		this.tv_num = (TextView) findViewById(R.id.tv_num);
		this.ll_ok = (LinearLayout) findViewById(R.id.ll_ok);
		this.ll_ok.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				ArrayList<String> list = new ArrayList();
				for (String str : ImageGridActivity.this.adapter.map.values()) {
					list.add(str);
				}
				for (int i = 0; i < list.size(); i++) {
					if (Bimp.drr.size() < 10) {
						Bimp.drr.add(list.get(i));
					}
				}
				if (Bimp.act_bool) {
					ImageGridActivity.this.setResult(-1);
					ImageGridActivity.this.finish();
				}
			}
		});
	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View arg1, int arg2, long arg3) {
		this.adapter.notifyDataSetChanged();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back /* 2131165198 */:
			Intent intent = new Intent(this, AlbumListActivity.class);
			intent.putExtra("listBuckets", (Serializable) this.listBuckets);
			startActivityForResult(intent, this.albumRequest);
		case R.id.btn_index /* 2131165213 */:
			finish();
		default:
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == this.albumRequest) {
			if (resultCode == 0) {
				finish();
			}
			if (resultCode == -1) {
				this.index = data.getIntExtra("index", this.index);
				this.listItems = this.listBuckets.get(this.index).imageList;
				this.adapter.setDataList(this.listItems);
				this.adapter.notifyDataSetChanged();
			}
		}
	}
}